Page({
  data: {
    id:null,
    customerinfo: {},
    info: {
			CustomerDemandId: null,
			ActualMeasureDate: '请选择量房时间',
			MeasureId: null,
			MeasureName: "请选择量房人员"
    },
    currentTab: 1,
    focus:[false,false],
    option: "编辑",
    measureList: [],
    measure_i: null,
    contactList: [],
    contact_i: null,
    startX: null,
    connectList: [],
    showlf: false,
    currentid: null,
    reason: '',
    x: 50,
    y: 10
  },
  onLoad(e) {
    console.log(e);
    var _this = this
    var newinfo = this.data.info
    newinfo.CustomerDemandId = e.id
    this.setData({id:e.id,info:newinfo})
    this.getInfo()
    dd.getSystemInfo({
      success: (res) => {
        _this.setData({
          x: res.windowWidth - 80,
          y: res.windowHeight - 130
        })
      }
    })
  },
  onShow() {
    this.getInfo()
  },
  getInfo(){
    var _this = this
    getApp().req({
      url: "/customer/customerdetail/"+this.data.id,
      cb(res){
        console.log(res)
        _this.setData({customerinfo:res.data})
        getApp().req({
          url: "/customer/contactrecordlist/"+_this.data.customerinfo.CustomerId,
          cb(res1){
            var list = res1.data.Items.filter(function(e){
              return e.CustomerDemandId == _this.data.id
            })
            _this.setData({connectList:list})
          }
        })
        getApp().req({
          url: "/customer/contactlist/"+_this.data.customerinfo.CustomerId,
          cb(res1){
            var list1 = res1.data.Items.map(function(e){
              e.x = 0
              return e
            })
            _this.setData({contactList:list1})
          }
        })
      }
    })
    getApp().req({
      url: "/customer/customermeasure/"+this.data.id,
      cb(res){
        var list = res.data.Items.map(function(e){
          e.x = 0
          return e
        })
        _this.setData({measureList:list})
      }
    })
  },
  lfplan() {
    this.setData({
      showlf:true,
      info:{
        CustomerDemandId: this.data.id,
        ActualMeasureDate: '请选择量房时间',
        MeasureId: null,
        MeasureName: '请选择量房人员'
      }}
    )
    console.log(this.data.showlf);
    
  },
  delcontact(e) {
    var _this = this
    console.log(e);
    
    dd.confirm({
      title: '提示',
      content: '确定删除此联系人?',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (res) => {
        console.log(res);
        if(res.confirm){
          getApp().req({
            url: "/customer/contact/delete",
            type: "POST",
            data: {CustomerContactId:e.target.dataset.id},
            cb(res){
              _this.getInfo()
            }
          })
        }
      },
    });

  },
  showdel(e) {
    var _this = this
    console.log(e);
    
    this.setData({
      currentid: e.target.dataset.id,
      reason: ""
    })
    dd.confirm({
      title: '提示',
      content: '确定作废此操作?',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (res) => {
        console.log(res);
        if(res.confirm){
          _this.setData({showdel:true})
        }else(
          this.setData({
            currentid: null,
            reason: ""
          })
        )
      },
    });
  },
  changeReason(e){
    this.setData({reason:e.detail.value})
  },
  delcancel() {
    this.setData({
      showdel:false
    })
  },
  delcheck() {
    var _this = this
    if(!this.data.reason){
      dd.showToast({
        type: "fail",
        content: "请输入原因"
      })
      return;
    }
    getApp().req({
      url: "/customer/customermeasurecancel/" + this.data.currentid,
      type: "POST",
      data: {Reason:this.data.reason},
      cb(res){
        _this.setData({
          showdel:false
        })
        _this.getInfo()
      }
    })
  },
  timeslt(e) {
    dd.hideKeyboard();
    var newinfo = this.data.info
    var keyname = e.target.dataset.name
    var _this = this
    dd.datePicker({
      format: 'yyyy-MM-dd HH:mm',
      currentDate: !newinfo[keyname]||newinfo[keyname]=='请选择量房时间'?'':newinfo[keyname],
      success: (res) => {
        newinfo[keyname] = res.date
        _this.setData({info: newinfo})
      },
    });
  },
  toemp(e) {
    dd.hideKeyboard();
    dd.navigateTo({
      url: "/pages/marks/empslt/empslt?role=" + e.target.dataset.name
    })
  },
  lfcancel() {
    this.setData({
      showlf:false
    })
  },
  lfcheck() {
    var _this = this
    if(!this.data.info.ActualMeasureDate || this.data.info.ActualMeasureDate == '请选择量房时间'){
      dd.showToast({
        type: "fail",
        content: "请选择一个时间"
      })
      return;
    }
    if(!this.data.info.MeasureId || this.data.info.MeasureId == '请选择量房人员'){
      dd.showToast({
        type: "fail",
        content: "请选择一个一名员工"
      })
      return;
    }
    getApp().req({
      url: "/customer/customeredit/3",
      type: "POST",
      data: this.data.info,
      cb(res){
        _this.setData({
          showlf:false
        })
        _this.getInfo()
      }
    })
  },
  moveStart(e) {
    var listname = e.target.dataset.list
    var list = this.data[listname+"List"]
    if(this.data[listname+"_i"] == e.target.dataset.i){

    }else{
      for (const i in list) {
          list[i].x = 0
      }
      list[e.target.dataset.i].x = -1
      this.setData({
        [listname+"List"]: list,
        [listname+"_i"]: e.target.dataset.i
        })
    }
    this.setData({
      startX:e.changedTouches[0].clientX
    })

  },
  moveend(e) {
    var listname = e.target.dataset.list
    var list = this.data[listname+"List"]
    var x = e.changedTouches[0].clientX - this.data.startX
    var random = Math.random()*-1
    if(x<-50){
      list[e.target.dataset.i].x = -60+random
    }else{
      list[e.target.dataset.i].x = 0
    }
    this.setData({[listname+"List"]:list})
    
  },
  swiperTab(e) {

    console.log(e)
    this.setData({currentTab:e.target.dataset.index})
    switch (this.data.currentTab) {
      case "1": this.setData({option:"编辑"})
        break;
      case "2": this.setData({option:"编辑"})
        break;
      case "3": this.setData({option:"添加"})
        break;
      case "4": this.setData({option:"添加"})
        break;
      default: 
        break;
    }
  },
  option() {
    console.log(this.data.id)
    switch (+this.data.currentTab) {
      case 1: 
        dd.navigateTo({
          url:"../crmcustomeradd/crmcustomeradd?id="+this.data.id
        })
        break;
      case 2: 
        dd.navigateTo({
          url:"../crmcustomeradd/crmcustomeradd?id="+this.data.id
        })
        break;
      case 3: 
        dd.navigateTo({
          url:"./followupadd/followupadd?id="+this.data.customerinfo.CustomerId+"&did="+this.data.customerinfo.CustomerDemandId
        })
        break;
      case 4: 
        dd.navigateTo({
          url:"./contactadd/contactadd?id="+this.data.customerinfo.CustomerId
        })
        break;
      default: 
        break;
    }

  }
});
